package com.menusifu.cloudmenu.service.impl;

import com.menusifu.cloudmenu.converter.CategoryAndCategoryDTOConverter;
import com.menusifu.cloudmenu.dao.CategoryDao;
import com.menusifu.cloudmenu.dto.CategoryDTO;
import com.menusifu.cloudmenu.dto.SaleItemDTO;
import com.menusifu.cloudmenu.dto.TaxDTO;
import com.menusifu.cloudmenu.entity.Category;
import com.menusifu.cloudmenu.entity.SaleItem;
import com.menusifu.cloudmenu.service.CategoryService;
import com.menusifu.cloudmenu.service.SaleItemService;
import com.menusifu.cloudmenu.service.TaxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author xie
 * @date 2019/1/15 14:49
 * @description:
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryDao categoryDao;

    @Autowired
    SaleItemService saleItemService;

    @Autowired
    TaxService taxService;

    @Override
    public List<CategoryDTO> getAllCategories() {
        List<Category> categories = categoryDao.getAllCategories();
        Map<String, List<SaleItemDTO>> saleItemDTOListMap = new HashMap<>();
        Map<String, TaxDTO> taxMap = new HashMap<>();
        for (Category category : categories) {
            List<SaleItemDTO> saleItemsByCategoryId = saleItemService.getSaleItemsByCategoryId(category.getId());
            if (!CollectionUtils.isEmpty(saleItemsByCategoryId)) {
                saleItemDTOListMap.put(category.getId(), saleItemsByCategoryId);
            }
            if (category.getTaxId() != null) {
                TaxDTO taxById = taxService.getTaxById(category.getTaxId());
                taxMap.put(category.getId(), taxById);
            }
        }
        return CategoryAndCategoryDTOConverter.converter(categories, saleItemDTOListMap, taxMap);
    }

    @Override
    public void addCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        List<SaleItemDTO> saleItemDTOs = categoryDTO.getSaleItems();
        if (!CollectionUtils.isEmpty(saleItemDTOs)) {
            categoryDTO.setSaleItems(saleItemService.batchAddSaleItems(saleItemDTOs));
        }
        if (categoryDTO.getTax() != null) {
            categoryDTO.setTax(taxService.addTax(categoryDTO.getTax()));
        }
        BeanUtils.copyProperties(categoryDTO, category);
        category.setLastUpdated(new Date());
        categoryDao.addCategory(category);
    }

    @Override
    public void updateCategory(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        category.setLastUpdated(new Date());
        categoryDao.updateCategory(category);
    }

    @Override
    public void deleteCategory(String id) {
        saleItemService.deleteSaleItemByCatId(id);
        categoryDao.deleteCategory(id);
    }

    @Override
    public CategoryDTO getCategoryById(String id) {
        List<SaleItemDTO> saleItemsByCategoryId = saleItemService.getSaleItemsByCategoryId(id);
        Category categoryById = categoryDao.getCategoryById(id);
        TaxDTO taxById = taxService.getTaxById(categoryById.getTaxId());
        return CategoryAndCategoryDTOConverter.converter(categoryById, saleItemsByCategoryId, taxById);
    }

    @Override
    public CategoryDTO copyCategory(String id) {
        CategoryDTO categoryDTO = getCategoryById(id);
        List<SaleItemDTO> saleItemDTOs = saleItemService.getSaleItemsByCategoryId(id);
        categoryDTO.setId(null);
        List<SaleItemDTO> newSaleItemDTOs = saleItemDTOs.stream().map(saleItemDTO -> {
            saleItemDTO.setCategoryId(null);
            saleItemDTO.setId(null);
            return saleItemDTO;
        }).collect(Collectors.toList());
        categoryDTO.setSaleItems(newSaleItemDTOs);
        addCategory(categoryDTO);
        return null;
    }
}
